<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>渐变式轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .lunbo {
            width: 1226px;
            height: 460px;
            margin: 0 auto;
            position: relative;
        }

        .lunbo > .imgLists {
            width: 100%;
            height: 100%;
        }

        .lunbo > .imgLists > .imgItem {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: all 1s;
        }

        .lunbo > .imgLists > .imgItem.active {
            opacity: 1;
        }

        .lunbo > .imgLists > .imgItem > img {
            width: 100%;
            height: 100%;
        }

        .lunbo > .preBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            left: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }

        .lunbo > .nextBtn {
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: calc(50% - 20px);
            right: 0;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="lunbo">
    <div class="imgLists">
        <div class="imgItem active"><img src="../img/mi1.jpg" alt=""></div>
        <div class="imgItem"><img src="../img/mi2.webp" alt=""></div>
        <div class="imgItem"><img src="../img/mi3.webp" alt=""></div>
        <div class="imgItem"><img src="../img/mi4.webp" alt=""></div>
        <div class="imgItem"><img src="../img/mi5.webp" alt=""></div>
    </div>
    <div class="preBtn"><</div>
    <div class="nextBtn">></div>
</div>
<script>
    var lunbo = document.querySelector(".lunbo");
    var preBtn = document.querySelector(".preBtn");
    var nextBtn = document.querySelector(".nextBtn");
    var imgItem = document.querySelectorAll(".imgItem");
    //左点击时间
    var currentImg = 0;//记录当前照片第几张
    preBtn.onclick = function (e) {
        //console.log(e);
        currentImg--;
        if (currentImg < 0) {
            currentImg = imgItem.length - 1;
        }
        //1.先将所有图片的active都出掉
        imgItem.forEach(function (item, index) {
            //console.log(item + index);
            item.classList.remove("active");
        })
        //2.讲下一个图片添加上active
        imgItem[currentImg].classList.add("active");
    }
    //右点击事件
    nextBtn.onclick = function (e) {
        //console.log(e);
        currentImg++;
        if (currentImg > imgItem.length - 1) {
            currentImg = 0;
        }
        //1.先将所有图片的active都出掉
        imgItem.forEach(function (item, index) {
            //console.log(item + index);
            item.classList.remove("active");
        })
        //2.讲下一个图片添加上active
        imgItem[currentImg].classList.add("active");
    }
</script>
</body>
</html>